package com.example.ceshi.SQLite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.ceshi.Bean.NowUserData;

import java.util.ArrayList;
import java.util.List;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "mySQLite.db";

    private static final String TABLE_NAME_USER = "user";

    private static final String TABLE_NAME_EXPRESS = "express";

    private static final String CREATE_TABLE_SQL = "create table " + TABLE_NAME_USER + " (id integer primary key autoincrement,username text,password text,showName text default null,sex integer default 1, maxNum integer default 0,imgPath text default null);\n";

    private static final String CREATE_TABLE_SQL2 = "create table " + TABLE_NAME_EXPRESS + " (id integer primary key autoincrement,expressNo text,userId integer);\n";

    public MySQLiteOpenHelper(Context context) {
        super(context, DB_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE_SQL);
        sqLiteDatabase.execSQL(CREATE_TABLE_SQL2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public long insertExpressData(String expressNo, Integer userId) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("expressNo", expressNo);
        values.put("userId", userId);

        return db.insert(TABLE_NAME_EXPRESS, null, values);

    }


    public int deleteExpressData(String expressNo, Integer userId) {
        SQLiteDatabase db = getWritableDatabase();

        return db.delete(TABLE_NAME_EXPRESS, "userId = ? and expressNo = ?", new String[]{userId.toString(), expressNo});

    }

    public long insertUserData(String username, String password) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", username);
        values.put("password", password);
        values.put("showName", "");
        values.put("sex", 1);
        values.put("maxNum",0);
        values.put("imgPath", "");
        return db.insert(TABLE_NAME_USER, null, values);
    }

    public long updateUserDate() {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", NowUserData.username);
        values.put("password", NowUserData.password);
        values.put("showName", NowUserData.showName);
        values.put("sex", NowUserData.sex);
        values.put("maxNum", NowUserData.maxNum);
        values.put("imgPath", NowUserData.imgPath);
        return db.update(TABLE_NAME_USER, values, "id = ?", new String[]{NowUserData.id.toString()});
    }


    @SuppressLint("Range")
    public boolean selectUserByUsername(String username) {
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query(TABLE_NAME_USER, null, "username = ? ", new String[]{username}, null, null, null);
        if (cursor != null) {
            if (cursor.moveToNext()){
                return false;
            }
            cursor.close();

        }
        return true;

    }

    public boolean selectUserByUsernameAndPassword(String username,String password){
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query(TABLE_NAME_USER, null, "username = ? and password = ? ", new String[]{username ,password}, null, null, null);
        if (cursor.moveToNext()) {

                NowUserData.id = Integer.valueOf(cursor.getString(cursor.getColumnIndex("id")));
                NowUserData.username = cursor.getString(cursor.getColumnIndex("username"));
                NowUserData.password = cursor.getString(cursor.getColumnIndex("password"));
                NowUserData.showName = cursor.getString(cursor.getColumnIndex("showName"));
                NowUserData.sex = Integer.valueOf(cursor.getString(cursor.getColumnIndex("sex")));
                NowUserData.maxNum = Integer.valueOf(cursor.getString(cursor.getColumnIndex("maxNum")));
                NowUserData.imgPath= cursor.getString(cursor.getColumnIndex("imgPath"));
                NowUserData.dataDetailList=null;

            return true;
        }
        return false;
    }

    public List<String> selectExpressNoByUsername(String id) {
        SQLiteDatabase db = getWritableDatabase();
        List<String> results = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME_EXPRESS, null, "userId = ? ", new String[]{id}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String s = cursor.getString(cursor.getColumnIndex("expressNo"));

                results.add(s);
            }
        }
        return results;
    }

    public String selectExpressNoByExpressNo(String expressNo) {
        SQLiteDatabase db = getWritableDatabase();
        String s = "false";
        Cursor cursor = db.query(TABLE_NAME_EXPRESS, null, "userId = ? and expressNo = ?", new String[]{NowUserData.id.toString(), expressNo}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                s = "success";
            }
        }
        return s;
    }

}
